package nettyTest.netty3Test.factorial;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.compression.ZlibCodecFactory;
import io.netty.handler.codec.compression.ZlibWrapper;
import io.netty.handler.ssl.SslContext;

public class FactorialClientInitializer extends ChannelInitializer<SocketChannel> {

      private final SslContext sslCtx;
 
      public FactorialClientInitializer(SslContext sslCtx) {
         this.sslCtx = sslCtx;
      }
  
      @Override
      public void initChannel(SocketChannel ch) {
          ChannelPipeline pipeline = ch.pipeline();
  
          if (sslCtx != null) {
              pipeline.addLast(sslCtx.newHandler(ch.alloc(), FactorialClient.HOST, FactorialClient.PORT));
          }
  
        // Enable stream compression (you can remove these two if unnecessary)
          pipeline.addLast(ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP));
        pipeline.addLast(ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP));
  
         // Add the number codec first,
          pipeline.addLast(new BigIntegerDecoder());
          pipeline.addLast(new NumberEncoder());
  
         // and then business logic.
          pipeline.addLast(new FactorialClientHandler());
      }
}
